package cn.gz.demo.ui;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.WindowManager;

import androidx.databinding.DataBindingUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.gz.demo.R;
import cn.gz.demo.databinding.SystemBinding;
import cn.gz.demo.ui.base.BaseActivity;
import cn.gz.demo.utils.LogUtil;
import cn.gz.demo.utils.ToastUtil;

/**
 * Created by Lyz on 2017/3/8.
 */
public class SystemActivity extends BaseActivity {
    private SystemBinding binding;
    private AudioManager mAudioManager;
    private int maxVolume, currentVolume;
    int value = 10;
    Random random;
    public static List<MediaPlayer> playerList = new ArrayList<>();
    public MediaPlayer mediaPlayer = null;
    public int resourceIds[] = new int[]{R.raw.distinguish_fail, R.raw.person_no_register, R.raw.distinguish_timeout, R.raw.pay_fail, R.raw.pay_success,
            R.raw.face_camera, R.raw.cancel_deal, R.raw.not_net_excess, R.raw.face_success};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_system);

        //获取系统的Audio管理者
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        //最大音量
        maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        //当前音量
        currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        initView();
        initMediaPlayer();
    }

    private void initMediaPlayer() {
        random = new Random();
        for (int i = 0; i < resourceIds.length; i++) {
            mediaPlayer = MediaPlayer.create(context, resourceIds[i]);
            mediaPlayer.setLooping(false);
            playerList.add(mediaPlayer);
        }

        binding.volume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showToast(context, ".......volume..........");
            }
        });
        binding.play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopVibrato();
                int type = random.nextInt(resourceIds.length);
                LogUtil.showLog("......type....." + type);
                startVibrato(type);
            }
        });
        binding.stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

    /**
     * 播放
     *
     * @param type
     */
    public void startVibrato(int type) {
        if (playerList.get(type) != null) {
            playerList.get(type).start();
        }
    }

    public void stopVibrato() {
        for (MediaPlayer mediaPlayer : playerList) {
            if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                mediaPlayer.seekTo(0);
            }
        }
    }

    private void initView() {

        binding.volume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ++value;
                LogUtil.showLog("........." + value);
                value = (int) ((value / 10.0 * maxVolume) % maxVolume);
                LogUtil.showLog("........." + value);
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, value, 0);
            }
        });
        binding.brightness.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isAutoBrightness(SystemActivity.this)) {
                    autoBrightness(SystemActivity.this, false);
                }
                ++value;
                LogUtil.showLog("....11....." + getScreenBrightness(SystemActivity.this));
                LogUtil.showLog("........." + value);
                saveBrightness(SystemActivity.this, value);
            }
        });
    }

    /**
     * 判断是否开启了自动亮度调节
     *
     * @param context
     * @return
     */
    public static boolean isAutoBrightness(Context context) {
        ContentResolver resolver = context.getContentResolver();
        boolean automicBrightness = false;
        try {
            automicBrightness = Settings.System.getInt(resolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        return automicBrightness;
    }

    /**
     * 获取当前屏幕亮度
     */

    public static int getScreenBrightness(Context context) {
        int nowBrightnessValue = 0;
        ContentResolver resolver = context.getContentResolver();
        try {
            nowBrightnessValue = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nowBrightnessValue;
    }

    /**
     * 关闭自动亮度调节
     */
    public static boolean autoBrightness(Context activity, boolean flag) {
        int value = 0;
        if (flag) {
            value = Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; //开启
        } else {
            value = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;//关闭
        }
        return Settings.System.putInt(activity.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                value);
    }

    /**
     * 设置亮度，退出app也能保持该亮度值
     */
    public static void saveBrightness(Context context, int brightness) {
        ContentResolver resolver = context.getContentResolver();
        Uri uri = android.provider.Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
        android.provider.Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
        resolver.notifyChange(uri, null);
    }

    /**
     * 设置当前activity显示的亮度
     */
    public static void setBrightness(Activity activity, int brightness) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
        activity.getWindow().setAttributes(lp);
    }
}
